//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - SaaS Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace ConfigurationWizard
{
    public partial class Shell : Form
    {
        public event EventHandler<EventArgs> WizardCanceled;
        private bool operationCancelled;
        int context;

        public Shell()
        {
            InitializeComponent();
            
        }

        private void Shell_Load(object sender, EventArgs e)
        {
            LoadPanels();
        }

        private void NextButton_Click(object sender, EventArgs e)
        {
            context = context + 1;
            LoadPanels();
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            switch (context)
            {
                case 1:
                case 2:
                case 3:
                    CancelWizard();
                    break;
                case 4:
                    LoadPanels();
                    break;
                default:
                    Environment.Exit(0);
                    break;
            }
        }

        private void CancelWizard()
        {
            if (WizardCanceled != null)
                WizardCanceled(this, new EventArgs());

            Thread.Sleep(100);
            context = -1;
            LoadPanels();
            operationCancelled = true;
        }

        private void LoadPanels()
        {
            UserControl ctrl = null;

            switch (context)
            {
                case 0:
                    nextButton.Enabled = true;
                    ctrl = new Welcome();
                    break;
                case 1:
                    ctrl = new Information();                    
                    break;
                case 2:
                    nextButton.Enabled = true;
                    ctrl = new Checker();
                    break;
                case 3:
                    this.nextButton.Visible = false;
                    ctrl = new Progress();
                    Progress progressScreen = ctrl as Progress;
                    progressScreen.OperationFailed += new EventHandler<EventArgs>(OnOperationFailed);
                    progressScreen.OperationCompleted += new EventHandler<EventArgs>(OnOperationCompleted);
                    break;
                case 4:
                    nextButton.Enabled = false;
                    cancelButton.Text = "Close";
                    ctrl = new Summary();
                    context = -1;
                    break;
                case -1:
                    nextButton.Enabled = false;
                    cancelButton.Text = "Close";
                    ctrl = new Cancel();
                    break;
                default:
                    break;
            }
            if (ctrl != null)
            {
                ctrl.Dock = DockStyle.Fill;
                containerPanel.Controls.Clear();
                containerPanel.Controls.Add(ctrl);
            }
        }

        void OnOperationCompleted(object sender, EventArgs e)
        {
            if (operationCancelled)
                return;

            cancelButton.Text = "Finish";
            context = 4;
        }

        void OnOperationFailed(object sender, EventArgs e)
        {
            context = -1;
            LoadPanels();
        }       
    }
}

